/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.classification;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import edu.yonsei.iwb.smarttv.result.ResultClassificationInfo;
import edu.yonsei.iwb.smarttv.result.ResultNode;
import edu.yonsei.iwb.smarttv.result.SearchResultSet;
import java.util.Enumeration;
import java.util.Vector;

/**
 *
 * @author JDK
 */
public class SearchResultOrganizer {

    private Model model;
    private final Resource FILM_RESOURCE;
    private final Resource MUSICVIDEO_RESOURCE;
    private final Resource TV_EPISODE_RESOURCE;
    private ClassificationList clist;

    public SearchResultOrganizer(Model model) {
        this.model = model;
        this.FILM_RESOURCE = this.model.createResource("http://iweb.yonsei.ac.kr/ontologies/smarttv/Film");
        this.MUSICVIDEO_RESOURCE = this.model.createResource("http://iweb.yonsei.ac.kr/ontologies/smarttv/MusicVideo");
        this.TV_EPISODE_RESOURCE = this.model.createResource("http://iweb.yonsei.ac.kr/ontologies/smarttv/TvEpisode");
    }

    public void getClassificationInfo(Vector<SearchResultSet> srsSet) {
        for (int i = 0; i < srsSet.size(); i++) {
            SearchResultSet srs = srsSet.get(i);
            if (srs.getResultTypes().contains(this.FILM_RESOURCE)) {
//            if (srs.getResultType(0).equals(this.FILM_RESOURCE)) {
                this.clist = new ClassificationList(SearchResultType.FILM_TYPE);
                this.findClassifiedValueOfFilm(srs);
            } else if (srs.getResultTypes().contains(this.MUSICVIDEO_RESOURCE)) {
//            } else if (srs.getResultType(0).equals(this.MUSICVIDEO_RESOURCE)) {
                this.clist = new ClassificationList(SearchResultType.MUSICVIDEO_TYPE);
                this.findClassifiedValueOfMusicVideo(srs);
            } else if (srs.getResultTypes().contains(this.TV_EPISODE_RESOURCE)) {
//            } else if (srs.getResultType(0).equals(this.TV_EPISODE_RESOURCE)) {
                this.clist = new ClassificationList(SearchResultType.TV_EPISODE_TYPE);
                this.findClassifiedValueOfTvEpisode(srs);
            }

        }
    }

    private void findClassifiedValueOfFilm(SearchResultSet srs) {
//        for (int i = 0; i < this.clist.size(); i++) {
        Enumeration<String> e = clist.keys();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            Property classifier = this.model.createProperty(clist.get(key));
            StmtIterator s = srs.getResult().getResultNode().listProperties(classifier);
            while (s.hasNext()) {
                Statement st = s.nextStatement();
                Resource object;
                if (st.getObject().isResource()) {
                    object = st.getObject().asResource();
                } else {
                    object = this.model.createResource(st.getString());
                }
                ResultNode rn = new ResultNode(object);
                srs.setClassification(key, rn);
            }
        }
//        }
    }

    private void findClassifiedValueOfMusicVideo(SearchResultSet srs) {
//        for (int i = 0; i < this.clist.size(); i++) {
        Enumeration<String> e = clist.keys();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            Property classifier = this.model.createProperty(clist.get(key));
            StmtIterator s = srs.getResult().getResultNode().listProperties(classifier);
            while (s.hasNext()) {
                Statement st = s.nextStatement();
                Resource object;
                if (st.getObject().isResource()) {
                    object = st.getObject().asResource();
                } else {
                    object = this.model.createResource(st.getString());
                }
                ResultNode rn = new ResultNode(object);
                srs.setClassification(key, rn);
            }
        }
//        }
    }

    private void findClassifiedValueOfTvEpisode(SearchResultSet srs) {
        Property tv_episode_program = this.model.createProperty("http://iweb.yonsei.ac.kr/ontologies/smarttv/tv/tv_program__episodes");
        Property tv_network_duration_network = this.model.createProperty("http://iweb.yonsei.ac.kr/ontologies/smarttv/tv/tv_network_duration__network");
//        Property tv_episode_program = this.model.createProperty("http://iweb.yonsei.ac.kr/ontologies/smarttv/tv/tv_series_episode/series");

        StmtIterator tv_program_stmt = this.model.listStatements(null, tv_episode_program, srs.getResult().getResultNode());
        while (tv_program_stmt.hasNext()) {
            Statement s1 = tv_program_stmt.nextStatement();
            Resource tv_program = s1.getSubject().asResource();
//            for (int i = 0; i < this.clist.size(); i++) {
            Enumeration<String> e = clist.keys();
            while (e.hasMoreElements()) {
                String key = e.nextElement();
                Property classifier = this.model.createProperty(clist.get(key));
                StmtIterator s = tv_program.listProperties(classifier);
                while (s.hasNext()) {
                    Statement st = s.nextStatement();
                    Resource object;
                    if (st.getObject().isResource()) {
                        object = st.getObject().asResource();
                    } else {                        
                        object = this.model.createResource(st.getString());
                    }
                    
                    if(classifier.toString().equals(ClassificationList.ORIGINAL_NETWORK)){
                        StmtIterator s2 = object.listProperties(tv_network_duration_network);
                        Statement st2 = s2.nextStatement();
                        object = st2.getObject().asResource();
                    }
                    
                    ResultNode rn = new ResultNode(object);
                    srs.setClassification(key, rn);
                }
            }
//            }
        }
    }
}
